package com.code.google.mdsc.gui;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import com.code.google.mdsc.control.Business;
import com.code.google.mdsc.gui.i18n.Messages;

public class SearchWindow {

	private static final int INSET = 10;
	private final JDialog dialog;

	public SearchWindow(final Business business, final MainWindow mw) {

		dialog = new JDialog(mw.getMainFrame(),
				Messages.getString("SEARCH_TITLE"));
		Container c = dialog.getContentPane();

		c.setLayout(new GridBagLayout());

		final JLabel accountHolderLabel = new JLabel(
				Messages.getString("SEARCH_ACCOUNT_HOLDER"));
		final JLabel ibanLabel = new JLabel(Messages.getString("SEARCH_IBAN"));
		final JLabel bicLabel = new JLabel(Messages.getString("SEARCH_BIC"));

		final JTextField accountHolder = new JTextField("");
		final JTextField iban = new JTextField("");
		final JTextField bic = new JTextField("");

		JButton search = new JButton(Messages.getString("SEARCH_DO"));
		JButton cancel = new JButton(Messages.getString("SEARCH_CANCEL"));
		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dialog.dispose();
			}
		});

		search.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String[] values = { accountHolder.getText(), iban.getText(),
						bic.getText() };

				business.search(mw, values);
				dialog.dispose();
			}
		});

		GridBagConstraints gbc = new GridBagConstraints();

		Insets in = new Insets(INSET, INSET, INSET, INSET);

		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.insets = in;
		gbc.gridy = 0;
		gbc.gridx = 0;

		c.add(accountHolderLabel, gbc);

		gbc.gridx = 1;
		c.add(accountHolder, gbc);
		gbc.gridy++;
		gbc.gridx = 0;
		c.add(bicLabel, gbc);

		gbc.gridx = 1;
		c.add(bic, gbc);
		gbc.gridy++;
		gbc.gridx = 0;
		c.add(ibanLabel, gbc);

		gbc.gridx = 1;
		c.add(iban, gbc);

		gbc.gridy++;
		gbc.gridx = 0;
		c.add(search, gbc);

		gbc.gridx = 1;
		c.add(cancel, gbc);

		dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		dialog.pack();
		dialog.setResizable(false);
		dialog.setVisible(true);
	}
}
